import { strategy as StrategyApi } from '../../../../../config/request'
const repeatType = [
    { value: 0, label: '无限制' },
    { value: 1, label: '一天' },
    { value: 2, label: '一周' },
    { value: 3, label: '一个月' },
    { value: 4, label: '一年' },
]
module.exports = {
    name: 'strategy',
    data() {
        return {
            disabled: true,
            addFormVisible: false,
            tableData: [],
            strategyForm: {},
            repeat_types: [
                { value: 0, label: '无限制' },
                { value: 1, label: '一天' },
                { value: 2, label: '一周' },
                { value: 3, label: '一个月' },
                { value: 4, label: '一年' },
            ],
            fields: [{
                    info: { prop: 'name', label: '策略名称', sortable: false },
                    filter: {},
                    style: { align: 'center' }
                },
                //  {
                //     info: { prop: 'describe', label: '策略描述', sortable: false },
                //     filter: {},
                //     style: { align: 'center' }
                // }
                {
                    info: { prop: 'integral', label: '增加积分', sortable: false },
                    filter: {},
                    style: { align: 'center' }
                },
                {
                    info: { prop: 'repeat_number', label: '最大重复次数(0代表无上限)', sortable: false },
                    filter: {},
                    style: { align: 'center' }
                }
            ],
            status: {
                info: { prop: 'status', label: '策略状态', sortable: true },
                filter: {
                    list: [{
                        text: '启用',
                        value: 1
                    }, {
                        text: '禁止',
                        value: 0
                    }],
                    multiple: false
                },
                style: { align: 'center' }
            },
            repeat_type: {
                info: { prop: 'repeat_type', label: '重复周期', sortable: false },
                filter: {
                    list: [{
                        text: '无限制',
                        value: 0
                    }, {
                        text: '一天',
                        value: 1
                    }, {
                        text: '一周',
                        value: 2
                    }, {
                        text: '一个月',
                        value: 3
                    }, {
                        text: '一年',
                        value: 4
                    }],
                    multiple: false
                },
                style: { align: 'center' }
            },
        }
    },
    methods: {
        /**
         * 格式化状态
         */
        formatterStatu(item) {
            return item.status == 1 ? '启动' : '禁止';
        },
        /**
         * 过滤状态
         */
        filterStatu(value, item) {
            return item.status == value;
        },

        /**
         * 过滤策略状态
         */
        filterRepeatType(value, item) {
            return item.repeat_type == value;
        },
        /**
         * 格式化策略状态
         * 
         * @param {any} item 
         * @returns 
         */
        formatterRepeatType(item) {
            for (let i in repeatType) {
                if (item.repeat_type == repeatType[i].value) {
                    return repeatType[i].label
                }
            }
        },
        checkLevel() {
            var user = this.$store.state.user.userinfo;
            // console.log('user', user);
            return user && user.level == 1;
        },
        _getAllStrategy() {
            StrategyApi.queryAll.call(this, data => {
                this.tableData = data['strategys'];

                data.strategys.forEach(strategy => {
                    Object.assign(strategy, { checked: strategy.status == 1 });
                });
                console.log(data)
            })
        },
        onUpdate(strategy) {
            this.strategyForm = strategy;
            this.addFormVisible = true;
        },
        /** 提交表单 */
        submitForm(formName) {
            var strategy = {};
            Object.assign(strategy, this[formName]);
            console.log(strategy)
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this._updateStrategy(strategy);
                } else {
                    return false;
                }
            });
        },
        _updateStrategy(strategy) {
            StrategyApi.updateStrategy.call(this, strategy, data => {
                this.$message.success('修改成功');
                this.addFormVisible = false;
            })
        },
        onCheckedChange(strategy) {
            StrategyApi.updateStrategyStatus.call(this, {
                id: strategy.id,
                status: strategy.checked ? 1 : 0
            }, data => {
                strategy.status = data.strategy.status;
                this.$message.success(data.strategy.status == 1 ? '已经启用' : '已经禁用');
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    },
    created: function() {
        this._getAllStrategy();
    }

}